package lock;

import java.util.concurrent.locks.ReentrantLock;

/*
3个窗口卖100张票
 */
public class Test5 {
    public static void main(String[] args) {
        TicketTask5 task = new TicketTask5();
        Thread t1 = new Thread(task,"窗口1");
        Thread t2 = new Thread(task,"窗口2");
        Thread t3 = new Thread(task,"窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}
class TicketTask5 implements Runnable {
    private int tikets = 100;

    private ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            lock.lock();//上锁
            if (tikets<=0){
                //防止循环停了unlock没执行，或者finally语句
                lock.unlock();
                break;
            }
            System.out.println(Thread.currentThread().getName()+"卖出了第"+tikets+"张票");
            tikets--;
            lock.unlock();//释放锁
        }

    }
}